﻿#include "TopTitle.h"
#include <QHBoxLayout>
#include <QMouseEvent>
#include "CustomButton.h"
#define TOP_DEFAULT_HEIGHT  35  // 标题栏高度

// 布局：收缩按钮|标题信息    刷新重置
TopTitle::TopTitle(QWidget* parent)
    : QFrame(parent)
    , m_bWindowNormal(true)
    , m_pTitleInfo(nullptr)
{
    this->setFixedHeight(TOP_DEFAULT_HEIGHT);

    QHBoxLayout* pBox = new QHBoxLayout;
    setLayout(pBox);
    pBox->setSpacing(0);
    pBox->setContentsMargins(0, 0, 0, 0);
    this->setStyleSheet(TOOL_COMMON_BACKGROUND_COLOUR);

    QPushButton* pShowBar = new QPushButton(this);
    pShowBar->setFixedSize(QSize(TOP_DEFAULT_HEIGHT, TOP_DEFAULT_HEIGHT));
    pShowBar->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    pShowBar->setIcon(QIcon(":/common/resource/common/set_color.ico"));
    pShowBar->setStatusTip(QString("展开"));

    m_pTitleInfo = new QLabel(this);
    m_pTitleInfo->setFixedWidth(200);
    m_pTitleInfo->setText("默认标题");

    pBox->addWidget(pShowBar);
    pBox->addWidget(m_pTitleInfo);
    pBox->addStretch();

    QPushButton* pShowTabs = new QPushButton(this);
    pShowTabs->setFixedSize(QSize(TOP_DEFAULT_HEIGHT, TOP_DEFAULT_HEIGHT));
    pShowTabs->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    pShowTabs->setIcon(QIcon(":/common/resource/common/align_center_alt_icon.ico"));
    pShowTabs->setStatusTip(QString("显示多标签栏"));
    pBox->addWidget(pShowTabs);
    
    m_pResizeWindow = new QPushButton(this);
    m_pResizeWindow->setFixedSize(QSize(TOP_DEFAULT_HEIGHT, TOP_DEFAULT_HEIGHT));
    m_pResizeWindow->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    m_pResizeWindow->setIcon(QIcon(":/common/resource/common/maximization.ico"));
    m_pResizeWindow->setStatusTip(QString("界面最大化"));
    pBox->addWidget(m_pResizeWindow);

    QPushButton* pCloseWindow = new QPushButton(this);
    pCloseWindow->setFixedSize(QSize(TOP_DEFAULT_HEIGHT, TOP_DEFAULT_HEIGHT));
    pCloseWindow->setStyleSheet("QPushButton { border: none; background-color: transparent; }");
    pCloseWindow->setIcon(QIcon(":/common/resource/common/close.ico"));
    pCloseWindow->setStatusTip(QString("关闭程序"));
    pBox->addWidget(pCloseWindow);

    connect(pShowBar, SIGNAL(clicked()), this, SLOT(slt_expand()));
    connect(pShowTabs, SIGNAL(clicked()), this, SLOT(slt_showTabs()));
    connect(m_pResizeWindow, SIGNAL(clicked()), this, SLOT(slt_resizeWindow()));
    connect(pCloseWindow, SIGNAL(clicked()), this, SLOT(slt_closeWindow()));
}

void TopTitle::setTitleInfo(const QString strTitle)
{
    m_pTitleInfo->setText(strTitle);
}

void TopTitle::slt_expand()
{
    emit click_expand();
}

void TopTitle::slt_closeWindow()
{
    emit click_closeWindow();
}

void TopTitle::slt_resizeWindow()
{
    m_bWindowNormal = !m_bWindowNormal;
    if (true == m_bWindowNormal)
    {
        m_pResizeWindow->setStatusTip(QString("界面最大化"));
        m_pResizeWindow->setIcon(QIcon(":/common/resource/common/maximization.ico"));
    }
    else
    {
        m_pResizeWindow->setStatusTip(QString("界面正常化"));
        m_pResizeWindow->setIcon(QIcon(":/common/resource/common/normal.ico"));
    }

    emit click_resizeWindow();
}

void TopTitle::slt_showTabs()
{
    emit click_showTabs();
}

void TopTitle::mousePressEvent(QMouseEvent* event)
{
    if (event->button() == Qt::LeftButton)
    {
        // 记录鼠标按下时的位置
        m_lastPos = event->globalPos();
        //setCursor(Qt::ClosedHandCursor);  // 启用后，鼠标悬停会改变鼠标的样式
    }
    QFrame::mousePressEvent(event);
}

void TopTitle::mouseMoveEvent(QMouseEvent* event)
{
    if (event->buttons() & Qt::LeftButton)
    {
        // 计算移动距离
        QPoint delta = event->globalPos() - m_lastPos;
        // 移动窗口
        window()->move(window()->pos() + delta);
        // 更新位置
        m_lastPos = event->globalPos();
    }
    QFrame::mouseMoveEvent(event);
}

void TopTitle::mouseDoubleClickEvent(QMouseEvent* event)
{
    // 在这里处理鼠标双击事件
    slt_resizeWindow();

    // 调用父类的mouseDoubleClickEvent()函数，以便继续处理该事件
    QFrame::mouseDoubleClickEvent(event);
}